package mt.dummy;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

import javax.swing.JTable;

import mt.Order;
import mt.comm.ClientComm;
import mt.comm.ClientSideMessage;
import mt.comm.ServerSideMessage.Type;
import mt.interfaceGui.ConfigTabelaHist;
import mt.interfaceGui.ConfiguracaoTabela;
import mt.tratapedidos.TrataPedidosCliente;



public class ClientCommDummy implements ClientComm {
	
	private final int PORTO= 8080;
	private Socket socket;
	private ObjectInputStream clienteSocketIn;
	private ObjectOutputStream clienteSocketOut;
	private String nickname;
	private LinkedBlockingQueue<ClientSideMessage> UpdateMessage = new LinkedBlockingQueue<ClientSideMessage>() ;
	private LinkedList<Order> sellingOrders = new LinkedList<Order>();
	private LinkedList<Order> buyingOrders = new LinkedList<Order>();
	private ConfiguracaoTabela tablemodel;
	private ConfigTabelaHist tableHist;


	/**
	 * @param serverAdress
	 * @param nickname
	 * Serve para conectar ao servidor
	 */
	@Override
	public void connect(String serverAddress, String nickname) {
		this.nickname=nickname;
		
		try {
			
			InetAddress endereco = InetAddress.getByName(serverAddress);
			
			
			socket= new Socket(endereco,PORTO);			
			
			clienteSocketIn=new ObjectInputStream(socket.getInputStream()) ;
			clienteSocketOut= new ObjectOutputStream(socket.getOutputStream() );
			
			clienteSocketOut.writeObject(new ServerSideMessageDummy(Type.CONNECTED,nickname,null));
			
			new TrataPedidosCliente(this,clienteSocketIn,clienteSocketOut).start();
			
		
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
	}

	@Override
	public boolean isConnected() {
		return socket.isConnected(); 
	}

	@Override
	public void disconnect() {
		try {
			
			clienteSocketOut.writeObject(new ServerSideMessageDummy(Type.DISCONNECTED, nickname, null));
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

	@Override
	public boolean hasNextMessage() {
		if(UpdateMessage.isEmpty()){
			return false;
		}
		return true;
	}

	@Override
	public ClientSideMessage getNextMessage() {
		try {
			return UpdateMessage.take();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void sendOrder(Order order) {
		ServerSideMessageDummy msg = new ServerSideMessageDummy(Type.NEW_ORDER,nickname, order);
		
		try {
			clienteSocketOut.writeObject(msg);
		
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public LinkedList<Order> getSellingOrders() {
		return sellingOrders;
	}

	public LinkedList<Order> getBuyingOrders() {
		return buyingOrders;
	}

	public void addMessagesToQueue(ClientSideMessageDummy message) {
		UpdateMessage.add(message);
	}

	
	public void table(ConfiguracaoTabela tableModel) {
		this.tablemodel=tableModel;
		
		
	}

	public void hist(ConfigTabelaHist configTabelaHist) {
		this.tableHist=configTabelaHist;
		
	}

	public void addOrderToTableModel(Order order) {
		tablemodel.add(order);
		
	}

	public void deleteOrderFromTableModel(Order entry) {
		tablemodel.delete(entry);
		
	}

	public void UpdateOrderOnTableModel(Order entry) {
		tablemodel.updateOrder(entry);
		
	}

	public void addOrderToHist(Order entry) {
		if(entry.getNickname().equals(nickname)){
			tableHist.add(entry);
		}
		
	}

	

	
}
